package com.dsiab.manageService.service.impl;

import com.dsiab.manageService.entity.MusicDto;
import com.dsiab.manageService.entity.PagesDto;
import com.dsiab.manageService.mapper.AdminDao;
import com.dsiab.manageService.mapper.PagesDao;
import com.dsiab.manageService.mapper.ToolsDao;
import com.dsiab.manageService.service.ToolsService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.lionsoul.ip2region.xdb.Searcher;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.system.ApplicationHome;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

@Slf4j
@Service
public class ToolsServiceImpl implements ToolsService {

  @Autowired
  PagesDao pagesDao;
  @Autowired
  AdminDao adminDao;
  @Autowired
  ToolsDao toolsDao;

  public String getAddressFromIp(String ip, boolean format) {
    // 获取当前应用目录，获取对应db文件，直接放到static目录下会提示找不到文件
    ApplicationHome ah = new ApplicationHome(this.getClass());
    String docStorePath = ah.getSource().getParentFile().toString();
    String dbPath = docStorePath + "/ip2region.xdb";

    Searcher searcher = null;
    String region = "";
    try {
      searcher = Searcher.newWithFileOnly(dbPath);
    } catch (java.io.IOException e) {
      System.out.printf("failed to create searcher with `%s`: %s\n", dbPath, e);
      return "";
    }
    //  String ip = "1.2.3.4";
    try {
      region = searcher.search(ip);
    } catch (Exception e) {
      log.info("获取ip失败");
    }
    if (format) {
      region = region.replace("0|","").replace("|","-").replace("-0","");
    }

    return region;
  }
  public static String getSymbol (String str) {
    String chinese = "[u4e00-u9fa5]";//汉字字符集+英文字符集+数字字符集
    Pattern p = Pattern.compile(chinese);
    Matcher m = p.matcher(str);
//    return m.replaceAll("");
    return m.replaceAll("");
  }
  public void deleteUrl() {
    List<PagesDto> list = pagesDao.deleteUrl();
    for (PagesDto pagesDto : list){
      String content = pagesDto.getContent().replace("<br/>","锕锕锕锕锕").replace("<!--HTMLBUILERPART0-->","").replace("<!--/HTMLBUILERPART0-->","").replace("</p>","").replace("<p>","").replace("\\n","").replace("１６３１６４ＣＯＭ","").replace("163164","");
//      String content = pagesDto.getContent().replace("<br/>","锕锕锕锕锕");
      String str = getSymbol(content);
      str = str.replace("锕锕锕锕锕","<br/>");
      pagesDto.setContent(str);
      pagesDto.setUid(UUID.randomUUID().toString());
      pagesDto.setCate(160);
      adminDao.insertOne(pagesDto);
      log.info("1111");
//      adminDao.updateOne(pagesDto);
    }
  }
  public Map getMusicList(int pageNum,int pageSize){
    Map<String, Object> resultMap = new HashMap<>();
    // 使用pageHelper分页
    PageInfo<MusicDto> musicDtoPageInfo;
    PageHelper.startPage(pageNum,pageSize);
    List<MusicDto> list = toolsDao.getMusicList(pageNum,pageSize);
    musicDtoPageInfo = new PageInfo<>(list);
    resultMap.put("total", musicDtoPageInfo.getTotal());
    resultMap.put("result", musicDtoPageInfo.getList());
    return resultMap;
  }

  public void insertMusicItem(MusicDto musicDto) {
    toolsDao.insertMusicItem(musicDto);
  }
  public void delMusicItem(int id) {
    toolsDao.delMusicItem(id);
  }
  public void updateMusicItem(MusicDto musicDto) {
    toolsDao.updateMusicItem(musicDto);
  }
}